﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace N2
{
	/// <summary>
	/// Holds information neccessary to bootstrap the N2 environment
	/// </summary>
	public class SessionSettings
	{
		public string ConfigurationFile { get; set; }

		public string AppRoot {
			get {
				return
					!string.IsNullOrEmpty(this.ConfigurationFile)
						? Path.GetDirectoryName(this.ConfigurationFile)
						: string.Empty;
			}
		}
		
		List<AssemblyName> m_assemblyList;
		
		public List<AssemblyName> AssemblyNameList {
			get {
				return this.m_assemblyList
					?? (this.m_assemblyList = new List<AssemblyName>());
			}
		}

		public IEnumerable<string> AssemblyFileNames
		{
			get {
				return this.AssemblyNameList
					.Select(_an => new Uri(_an.EscapedCodeBase).AbsolutePath);
			}
			set { this.ReplaceAssemblyNames(value); }
		}

		public void ReplaceAssemblyNames(IEnumerable<string> assemblies)
		{
			this.AssemblyNameList.Clear();
			this.AssemblyNameList.AddRange(
				from _name in assemblies
				where File.Exists(_name)
				select AssemblyName.GetAssemblyName(_name)
			);
		}
	}
}
